package cn.study.project.UpDownTextView;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.Transformation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

/**
 * @author zyy
 * @date 2021年05月16日 上午10:59
 */
public class UpDownTextView extends LinearLayout {

    private TextView tv;
    private ImageView iv;
    private int maxLine = 0;
    private boolean isExpand;

    public UpDownTextView(Context context) {
        this(context,null);
    }

    public UpDownTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.up_down_textview_layout,this);
        tv=findViewById(R.id.up_down_textView_tv);
        iv=findViewById(R.id.up_down_textView_iv);
    }

    public void setContent(String content){
        tv.setText(content);
        tv.setHeight(tv.getLineHeight()*maxLine);
        tv.post(new Runnable() {
            @Override
            public void run() {
                iv.setVisibility(tv.getLineCount()>maxLine? View.VISIBLE:View.GONE);
            }
        });
        setAnimation();
    }

    private void setAnimation(){
        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isExpand = ! isExpand;
                tv.clearAnimation();
                final int myHeight;
                final int startHeight=tv.getHeight();
                int myDurationTime=2000;
                if(isExpand){
                    myHeight = tv.getLineHeight()*tv.getLineCount() - startHeight;
                    RotateAnimation rotateAnimation=new RotateAnimation(0,180, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                    rotateAnimation.setDuration(myDurationTime);
                    rotateAnimation.setFillAfter(true);
                    iv.startAnimation(rotateAnimation);
                }else{
                    myHeight =tv.getLineHeight()*maxLine - startHeight;
                    RotateAnimation rotateAnimation=new RotateAnimation(180,0,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
                    rotateAnimation.setDuration(myDurationTime);
                    rotateAnimation.setFillAfter(true);
                    iv.startAnimation(rotateAnimation);
                }
                Animation myAnimation=new Animation() {
                    @Override
                    protected void applyTransformation(float interpolatedTime, Transformation t) {
                        tv.setHeight((int)(startHeight+myHeight*interpolatedTime));
                    }
                };
                myAnimation.setDuration(myDurationTime);
                tv.startAnimation(myAnimation);
            }
        });
    }


}
